package com.yeshine.opendata.controller.admin;

import com.yeshine.opendata.utils.TreeModel;
import com.yeshine.opendata.core.AppException;
import com.yeshine.opendata.entity.*;
import com.yeshine.opendata.service.*;
import com.yeshine.opendata.utils.JsonUtil;
import com.yeshine.opendata.utils.MD5Util;
import com.yeshine.opendata.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by taohj on 2014/7/7.
 */
@Controller
@RequestMapping({"/admin"})
public class AdminCommonController extends AbstractAdminControllerBase {


    @Autowired
    private IDataService dataService;

    @Autowired
    private IDictionaryService dictionaryService;

    @Autowired
    private ISysModelService sysModelService;

    @Autowired
    private IOrgService orgService;

    @Autowired
    private IDataThemeService dataThemeService;

    @Autowired
    private IDataSetService dataSetService;


    @Autowired
    private IAppThemeService appThemeService;

    @Autowired
    private ISysAdminService adminService;

    @Autowired
    private ISysRoleService sysRoleService;


    @RequestMapping({""})
    public String index() {
        return "admin/index";
    }


    @RequestMapping({"loginV"})
    public String loginV() {
        return "admin/login";
    }


    @ResponseBody
    @RequestMapping("login")
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        SysAdmin admin = new SysAdmin();
        admin.setUsername(username);
        admin.setPassword(password);
        Result<SysAdmin> result = adminService.login(admin);
        admin = result.getTarget();
        if (admin == null) {
            result.setMessage("用户名或密码不正确");
            result.setSuccess(false);
        } else {
            result.setSuccess(true);
            setSessionAttr("admin", result.getTarget());

            admin.setLastLoginTime(new Date());
            adminService.modify(admin);
        }
        return result.toString();
    }

    @ResponseBody
    @RequestMapping("getFunctionMenuTree")
    public String getFunctionMenuTree() {


        List<SysModel> models = null;

        SysAdmin admin = getCurrentUser();
        if (admin.getUsername().equals("admin")) {
            models = sysModelService.getList(null).getList();
        } else {
            models = sysRoleService.getPermission(getCurrentUser().getRole() != null ? getCurrentUser().getRole().getId() : 0);
        }


        List<TreeModel> treeModels = new ArrayList<TreeModel>();

        for (int i = 0; i < models.size(); i++) {
            SysModel model = models.get(i);
            TreeModel treeModel = new TreeModel();
            treeModel.setId(model.getId());
            treeModel.setText(model.getName());
            treeModel.setUrl(model.getUrl());
            treeModel.setAttributes(model);

            if (model.getParent() != null) {
                SysModel parent = model.getParent();
                treeModel.setParent(new TreeModel());
                treeModel.getParent().setId(parent.getId());
                treeModel.getParent().setText(parent.getName());

            }
            treeModels.add(treeModel);

        }


        return JsonUtil.toString(sysRoleService.getTreeModel(treeModels));
    }


    @ResponseBody
    @RequestMapping("setPassword")
    public String setPassword(
            @RequestParam("oldpassword") String oldpassword,
            @RequestParam("newpassword") String newpassword,
            @RequestParam("conformpassword") String conformpassword
    ) {
        SysAdmin admin = getCurrentUser();
        if (!StringUtils.hasLength(oldpassword)) {
            throw new AppException("原密码不能为空");
        }

        if (!StringUtils.hasLength(newpassword)) {
            throw new AppException("新密码不能为空");
        }

        if (!StringUtils.hasLength(conformpassword)) {
            throw new AppException("确认密码不能为空");
        }

        if (!newpassword.equals(conformpassword)) {
            throw new AppException("新密码与确认密码不一致");
        }

        if (!admin.getPassword().equals(MD5Util.MD5Code(oldpassword))) {
            throw new AppException("原密码不正确");
        }

        admin.setPassword(newpassword);
        Result<SysAdmin> result = adminService.modify(admin);
        if (result.isSuccess()){
            setCurrentUser(admin);
        }
        return result.toString();
    }


    @RequestMapping("loginOut")
    public String loginOut() {
        setCurrentUser(null);
        return "redirect:/admin";
    }


    @ResponseBody
    @RequestMapping("getDictionary")
    public String getDictionary(
            @RequestParam("type") String type
    ) {
        List<SysDictionary> list = dictionaryService.getList(new SysDictionary(type)).getList();
        SysDictionary sysDictionary = new SysDictionary();
        sysDictionary.setId(null);
        sysDictionary.setName("请选择");
        list.add(0, sysDictionary);
        return list.toString();
    }


    @ResponseBody
    @RequestMapping("getOrgs")
    public String getOrgs(
            @RequestParam(value = "org", required = false) Integer type
    ) {
        List<Org> list = orgService.getList(null).getList();
        Org org = new Org();
        org.setId(null);
        org.setName("请选择");
        list.add(0, org);
        return list.toString();
    }


    @ResponseBody
    @RequestMapping("getDataSets")
    public String getDataSets(
            @RequestParam(value = "org", required = false) Integer org
    ) {
        List<DataSet> list = dataSetService.getList(null).getList();
        DataSet ds = new DataSet();
        ds.setId(null);
        ds.setName("请选择");
        list.add(0, ds);
        return list.toString();
    }

    @ResponseBody
    @RequestMapping("getDataThemes")
    public String getDataThemes() {
        List<DataTheme> list = dataThemeService.getList(null).getList();
        DataTheme dataTheme = new DataTheme();
        dataTheme.setId(null);
        dataTheme.setName("请选择");
        list.add(0, dataTheme);
        return list.toString();
    }


    @ResponseBody
    @RequestMapping("getAppThemes")
    public String getAppThemes() {
        List<AppTheme> list = appThemeService.getList(null).getList();
        AppTheme appTheme = new AppTheme();
        appTheme.setId(null);
        appTheme.setName("请选择");
        list.add(0, appTheme);
        return list.toString();
    }


}
